<template>
  <div>
    <header>
      <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>商品管理</el-breadcrumb-item>
        <el-breadcrumb-item>商品分类</el-breadcrumb-item>
      </el-breadcrumb>
    </header>
    <main>
      <div class="title">
        <el-button type="primary" @click="addcategory">添加分类</el-button>
      </div>
      <el-table
        :data="list"
        style="width: 100%"
        border
        row-key="cat_id"
        :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
      >
        <el-table-column label="#" type="index">
          <template slot-scope="scope">
            <div v-if="scope.row.cat_level == 0||scope.row.cat_level==null">
              {{
              indexList.filter((item) => item.cat_id == scope.row.cat_id)[0].index
              }}
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="cat_name" label="分类名称"> </el-table-column>
        <el-table-column label="是否有效">
          <template slot-scope="scope">
            <i
              :class="{
                'el-icon-success': scope.row.cat_deleted === false,
                'el-icon-error': scope.row.cat_deleted === true,
                red: scope.row.cat_deleted === true,
                green: scope.row.cat_deleted === false,
              }"
            ></i>
          </template>
        </el-table-column>
        <el-table-column label="排序">
          <template>
            <el-tag size="mini" type="primary">一级</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button size="mini" @click="handleEdit(scope.$index, scope.row)"
              >编辑</el-button
            >
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.$index, scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <el-dialog title="添加分类" :visible.sync="dialogFormVisible">
        <el-form :model="form">
          <el-form-item
            label="分类名称"
            label-width="100px"
            :rules="{
              required: true,
              message: '请输入分类名称',
              trigger: 'blur',
            }"
          >
            <el-input v-model="form.name" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="父级分类" label-width="100px">
            <el-cascader
              :value="value"
              :options="options"
              :props="{ checkStrictly: true }"
              @change="handleChange"
            ></el-cascader>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="cancel">取 消</el-button>
          <el-button type="primary" @click="submitCategory">确 定</el-button>
        </div>
      </el-dialog>
    </main>
  </div>
</template>
<script>
import { getCategories, setCategories } from "@/api/goods";
export default {
  name: "ShopCategories",
  data() {
    return {
      list: [],
      hasChildren: true,
      indexList: [],
      dialogFormVisible: false,
      form: {
        name: "",
        father: "",
      },
      value: [],
      options: [],
    };
  },
  created() {
    this.getCategoriesList();
  },
  methods: {
    getPid() {
      if (this.value.length == 0) {
        return 0;
      } else if (this.value.length == 1) {
        return this.list.filter((item) => item.cat_id == this.value[0])[0]
          .cat_pid;
      } else if (this.value.length == 2) {
        return this.list
          .filter((item) => item.cat_id == this.value[0])[0]
          .children.filter((item) => item.cat_id == this.value[1])[0].cat_pid;
      } else if (this.value.length == 3) {
        return this.list
          .filter((item) => item.cat_id == this.value[0])[0]
          .children.filter((item) => item.cat_id == this.value[1])[0]
          .children.filter((item) => item.cat_id == this.value[2])[0].cat_pid;
      }
    },
    async submitCategory() {
      if (this.form.name == "") {
        return this.$message.error("请输入分类名称");
      }
      const pid = this.getPid();
      const data = {
        cat_level: this.value.length,
        cat_pid: pid,
        cat_name: this.form.name,
      };
      // console.log(data);
      const res = await setCategories(data);
      this.$message.success(res.data.meta.msg);
      this.form = {
        name: "",
        father: "",
      };
      this.value = [];
      this.dialogFormVisible = false;
    },
    cancel() {
      this.dialogFormVisible = false;
      this.form = {
        name: "",
        father: "",
      };
      this.value = [];
      this.$message.info("已取消删除");
    },
    async getCategoriesList() {
      const res = await getCategories();
      this.list = res.data.data;
      console.log(res.data.data)
      // console.log(this.list);
      this.indexList = this.list.map((item, index) => {
        return {
          cat_id: item.cat_id,
          index: index + 1,
        };
      });
      console.log(this.indexList)
    },
    handleEdit(index, row) {
      this.$message.error("让你编辑了吗,你就搁着瞎点!");
      console.log(index, row);
    },
    handleDelete(index, row) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
      })
        .then(() => {
          this.$message({
            type: "warning",
            message: "删删删,就知道删,是你添加的数据吗你就删",
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
      console.log(index, row);
    },
    addcategory() {
      this.getOptions();
      this.dialogFormVisible = true;
    },
    handleChange(value) {
      console.log(value);
      this.value = value;
    },
    getOptions() {
      this.list.forEach((item) => {
        if (item.children) {
          this.options.push({
            value: item.cat_id,
            label: item.cat_name,
            children: item.children.map((item) => {
              if (item.children) {
                return {
                  value: item.cat_id,
                  label: item.cat_name,
                  children: item.children.map((item) => {
                    return {
                      value: item.cat_id,
                      label: item.cat_name,
                    };
                  }),
                };
              } else {
                return {
                  value: item.cat_id,
                  label: item.cat_name,
                };
              }
            }),
          });
        } else {
          this.options.push({
            value: item.cat_id,
            label: item.cat_name,
          });
        }
      });
      // console.log(this.options);
    },
  },
};
</script>
<style lang='less'  scoped>
main {
  width: 100%;
  background-color: #fff;
  margin-top: 15px;
  padding: 20px;
  border-radius: 5px;
  box-shadow: 2px 2px 10px #ccc;
  .title {
    display: flex;
    margin-bottom: 20px;
    .el-input {
      width: 500px;
      margin-right: 20px;
    }
  }
  .red {
    color: red;
  }
  .green {
    color: #90ee90;
  }
}

::v-deep .el-table .el-icon-arrow-right:before {
  // content: "+";
  color: #1890ff;
  font-size: 20px;
  font-weight: 700px;
}
::v-deep .el-table .el-table__expand-icon .el-icon-arrow-right:before {
  content: "+";
}
::v-deep .el-table [class*="el-table__row--level"] .el-table__expand-icon {
  transform: rotate(0);
}
::v-deep
  .el-table
  .el-table__expand-icon--expanded
  .el-icon-arrow-right:before {
  content: "-";
}
::v-deep .el-table .el-table__placeholder::before {
  margin-left: 20px;
}
</style>
